<?php
namespace Manage\Controller;
use Common\Controller\ManageBaseController;
use Think\Model;
use Common\Model\ArticleModel;
use Common\Model\RenwuModel;
use Common\Model\MemberModel;
use Common\Model\JiluModel;



class FangkuanController extends ManageBaseController{
	/*
	 * 所有放款申请
	 */
	public function lists($ajax=null,$status=null,$page=null,$rows=null) {
		$map = array(); $status_view = array('default'=>'所有','del'=>'已删除','forbid'=>'禁用','allow'=>'正常');
		//查询条件 处理
		$now_status = $status_view['default'];
		if (isset($status) && key_exists($status, RenwuModel::$mystat)) { //指定查询状态
			$map['status'] = RenwuModel::$mystat[$status];
			$now_status = $status_view[$status];
		}else {
			$map['status'] = array('between',array(4,5)); //默认查询状态为未删除的数据
		}
		if ($page <= 0) {
			$page = 1;
		}
		if ($rows > 0) {
			$listRows = $rows;
		}else {
			$listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
		}
		
		//筛选条件
		$filter = I('param.');
		if(!empty($filter['cid'])){ //文章所属分类
			$map['cate']=$filter['cid'];
			$this->assign('cate', $map['cate']);
		}
		if ($filter['keyword']) {
			$map['title'] = array('like','%'.$filter['keyword'].'%');
			$this->assign('keyword', $filter['keyword']);
		}
		if ($filter['cate']>0) {
			$map['cate'] = $filter['cate'];
			$this->assign('cate', $filter['cate']);
		}else {
			$this->assign('cate', '0');
		}
		/******************/
		if ($ajax==='y') {
			$model = new Model('Renwu');
			$member = new MemberModel();
			$total = $model->where($map)->count(); //总记录数
			$list = $model->where($map)->order('updatetime desc')->page("$page,$listRows")->select();
			
			//list数据处理
			foreach ($list as &$v) {
				$v['mname'] = $member->where("id = ".$v['sid'])->getField("account");
				$v['time_type'] =date('Y-m-d H:i:s',$v['updatetime']); 
				if(!empty($v['jid'])){
					$v['jdname'] = $member->where("id = ".$v['jid'])->getField("account");
				}
			}
			unset($v);		
// 			print_r($list);
			$list = array("total"=>(int)$total,"rows"=>$list);
			$this->ajaxReturn($list);
		}else {
			$this->assign('_page', $page);
			$this->assign('_rows', $listRows);
			$this->assign('now_status',$now_status); //当前页面筛选的状态
			
			// 记录当前列表页的cookie
			cookie(C('CURRENT_URL_NAME'),$_SERVER['REQUEST_URI']);
			$this->display();
		}
	}
	
	/**
	 * 1、更改任务状态
	 * 2、增加对应金额到刷手账号
	 * 3、添加佣金发放记录
	 */
	public function state($id) {
		$id = (int)$id;
	
		$renwu = new Model('Renwu');
		$model = new Model();
		$member = new MemberModel();
		$jilu = new JiluModel();
		
		$info = $renwu->where("id = ".$id)->find();
		$fan = $info['yongjin'] + $info['price'];
		
		$map = array();//任务更新数据
		$map['updatetime'] = time();
		$map['status'] = 5;
		
		$model->startTrans();
		if (false === $renwu->where('`id`='.$id)->save($map)) {//更新任务  时间 状态
			$model->rollback();
			$this->error('任务信息更新失败!');
		}
		if (false === $member->where("id = ".$info['jid'])->setInc("total_money",$fan)){//更新 刷手总余额
			$model->rollback();
			$this->error('刷手信息更新失败!');
		}
		if (false === $member->where("id = ".$info['jid'])->setInc("able_money",$fan)){//更新 刷手可用余额
			$model->rollback();
			$this->error('刷手信息更新失败!');
		}
		$jilu_map = array();
		$jilu_map['cate'] = 5;
		$jilu_map['status'] = 1;
		$jilu_map['sid'] = $info['jid'];
		$jilu_map['createtime'] = time();
		$jilu_map['remark'] = '发放佣金和本金'.$info['price']." + ".$info['yongjin'];
		$jilu_map['money'] = $fan;
		$jilu_map['yue'] = $member->where("id = ".$info['jid'])->getField("total_money");
		if (false === $jilu->add($jilu_map)){//添加放款记录
			$model->rollback();
			$this->error('添加放款记录失败!');
		}
		$model->commit();
		$this->success('更新成功');
	}
}